package com.yige.web.controller

import com.mongodb.BasicDBObject
import com.yige.common.ResultUtil
import com.yige.service.mongoService.MongoActiveProgramService
import com.yige.service.mongoService.MongoActiveService
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.stereotype.Controller
import org.springframework.ui.Model
import org.springframework.web.bind.annotation.RequestMapping
import org.springframework.web.bind.annotation.RequestParam
import org.springframework.web.bind.annotation.ResponseBody

/**
 * Created by sunhao on 2016/7/8.
 */
@Controller
@RequestMapping("active")
class ActiveController {

    @Autowired
    MongoActiveService activeService
    @Autowired
    MongoActiveProgramService activeProgramService

    @RequestMapping("list.do")
    def list(Model model) {
        def list = activeService.findList()
        model.addAttribute("active", list)
        return "active/list"
    }

    @RequestMapping("addActive")
    @ResponseBody
    def addActive(String name, String info, int status) {
        def id = UUID.randomUUID().toString(), list = new ArrayList()
        def obj = [_id   : id, name: name, info: info, person: new ArrayList<>(),
                   status: status, income: 0, expend: 0, money: 0] as BasicDBObject
        boolean flag = this.activeService.insertOne(obj)
        if (flag) {
            this.activeProgramService.insertOne([_id: id, list: list] as BasicDBObject)
        }
        ResultUtil.recallAjax(flag)
    }

    @RequestMapping("updateActive")
    @ResponseBody
    def updateActive(String _id, String name, String info, int status) {
        def active_db = activeService.findOneById(_id)
        if (status == 9) {
            status = active_db.status
        }
        def obj = [_id   : _id, name: name, info: info, person: active_db.person, income: active_db.income,
                   status: status, expend: active_db.expend, money: active_db.money] as BasicDBObject
        boolean flag = this.activeService.updateOne(_id, obj)
        ResultUtil.recallAjax(flag)
    }

    @RequestMapping("delActive")
    @ResponseBody
    def delActive(@RequestParam('_ids') List<String> _ids) {
        boolean flag
        _ids.each { _id ->
            flag = false
            flag = this.activeService.deleteOne(_id)
        }
        ResultUtil.recallAjax(flag)
    }

    @RequestMapping("detail")
    def detail(String activeId, Model model) {
        def active = activeService.findOneById(activeId)
        model.addAttribute("active", active)
        return "active/detail"
    }

    @RequestMapping("program")
    def programList(Model model) {
        def obj = new BasicDBObject()
        def program = this.activeProgramService.findList()
    }

    @RequestMapping("add")
    @ResponseBody
    def addProgram(String activeId, String name, String info, double price) {
        def obj = [name: name, info: info, price: price] as BasicDBObject
        def program_db = activeProgramService.findOneById(activeId)
        def list = program_db.list as ArrayList
        list.add(obj)
        program_db.list = list
        def flag = activeProgramService.updateOne(activeId, program_db)
        ResultUtil.recallAjax(flag)
    }
}
